package com.gonsin.gview.example.test;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Slf4j
public class ChangeExt {

//    @Test
//    public void testBmp() throws IOException {
//        File path = new File("D:\\tools\\NameplateServer\\ReleaseDesktop\\TableCard\\TableCardXml\\Image");
//        File outPath = new File("D:\\tools\\NameplateServer\\ReleaseDesktop\\TableCard\\TableCardXml\\jpg");
//
//        File[] bmpFile = path.listFiles(new FilenameFilter() {
//            @Override
//            public boolean accept(File file, String s) {
//                boolean result = FilenameUtils.getExtension(s).equalsIgnoreCase("bmp");
//                return result;
//            }
//        });
//
//        for(File bmp : bmpFile){
//            File jpg = new File(outPath.getAbsolutePath() + File.separator + FilenameUtils.getBaseName(bmp.getName()) + ".jpg");
//            transBmpToJpg(bmp, jpg);
//        }
//
//    }

//    private void transBmpToJpg(File bmp, File jpg) throws IOException {
//        Image img = ImageIO.read(bmp);
//        BufferedImage tag = new BufferedImage(img.getWidth(null), img.getHeight(null),    BufferedImage.TYPE_INT_RGB);
//        tag.getGraphics().drawImage(img.getScaledInstance(img.getWidth(null), img.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
//        FileOutputStream out = new FileOutputStream(jpg);
//// JPEGImageEncoder可适用于其他图片类型的转换
//        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//        encoder.encode(tag);
//        out.close();
//    }


    @Test
    public void testXml() throws DocumentException, IOException {
        File path = new File("D:\\tools\\NameplateServer\\ReleaseDesktop\\TableCard\\TableCardXml\\YL\\Chinese");
        File outPath = new File("C:\\Users\\Administrator\\Downloads\\模板json数据_out");

        File[] xmlFile = path.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File file, String s) {
                boolean result = FilenameUtils.getExtension(s).equalsIgnoreCase("xml");
                return result;
            }
        });

        for(File xml : xmlFile){
            File json = new File(outPath.getAbsolutePath() + File.separator + FilenameUtils.getBaseName(xml.getName()) + ".json");
            transXmlToJson(xml, json);
        }

    }


    @Test
    public void testOneXml() throws DocumentException, IOException {
        File xml = new File("D:\\tools\\NameplateServer\\ReleaseDesktop\\TableCard\\TableCardXml\\YL\\Chinese\\A_TEMPLATE_PARTY_BUILD.xml");
        File json = new File("C:\\Users\\Administrator\\Downloads\\模板json数据_out\\A_TEMPLATE_PARTY_BUILD.json");
        transXmlToJson(xml, json);
    }


    public void transXmlToJson(File xml, File json) throws IOException, DocumentException {


        JsonObject newJson = new JsonObject();
        JsonArray components = new JsonArray();

//        File file = new File("D:\\tools\\NameplateServer\\ReleaseDesktop\\TableCard\\TableCardXml\\YL\\Chinese\\H_SYSTEM_TEMP4.xml");
        Document document = Dom4jUtils.parse(xml.toURL());
        Iterator<Node> childs = document.nodeIterator();
        while(childs.hasNext()){
            Element controls = (Element) childs.next();
            Iterator<Node> child = controls.nodeIterator();
            String backColor = controls.attributeValue("PanelMainBackColor");
            String backImage = controls.attributeValue("PanelMainBackImage");
            Integer width = Integer.valueOf(controls.attributeValue("PanelMainWidth"));
            Integer height = Integer.valueOf(controls.attributeValue("PanelMainHeight"));

            float pyY = (float) (480d / height);
            float pyX = (float) (800d / width);


            newJson.addProperty("height", 480);
            newJson.addProperty("width", 800);
            if(StringUtils.isNotEmpty(backImage)){
                String ext = FilenameUtils.getExtension(backImage);
                if(ext.equalsIgnoreCase("jpg")){
                    newJson.addProperty("backImage",
                            String.format("/imgs/temp_img/%s",
                                    backImage));
                } else {
                    newJson.addProperty("backImage",
                            String.format("/imgs/temp_img/%s.jpg",
                                    backImage.replace(".bmp", "")));
                }
            }

            if(StringUtils.isNotEmpty(backColor)){
                if(backColor.equalsIgnoreCase("white")){
                    backColor = "#FFFFFF";
                    newJson.addProperty("backColor", backColor);
                } else if(backImage.equalsIgnoreCase("black")){
                    backColor = "#000000";
                    newJson.addProperty("backColor", backColor);
                } else {
                    newJson.addProperty("backColor", backColor);
                }
            } else {
                newJson.addProperty("backColor", "#FFFFFF");
            }

            while(child.hasNext()){
                Node c = child.next();
                if(c instanceof Element){
                    log.info(c.getName());
                    if(c.getName().equalsIgnoreCase("SmartLabels")){
                        Iterator<Node> smartLabels = ((Element) c).nodeIterator();
                        while(smartLabels.hasNext()){
                            Node label = smartLabels.next();
                            if(label instanceof Element){



                                Element labelElement = (Element) label;
                                log.info(label.getName());
                                String name = labelElement.attributeValue("Name");
                                String text = labelElement.attributeValue("Text");
                                String textAlign = labelElement.attributeValue("TextAlign");
                                int itemWidth = (int) (Integer.parseInt(labelElement.attributeValue("Width")) * pyX);
                                int itemHeight = (int) (Integer.parseInt(labelElement.attributeValue("Height")) * pyY);
                                int posX = (int) (Integer.parseInt(labelElement.attributeValue("PosX")) * pyX);
                                int posY = (int) (Integer.parseInt(labelElement.attributeValue("PosY")) * pyY);
//                                String fontSize = String.valueOf((Float.parseFloat(labelElement.attributeValue("FontSize")) * pyY));
                                String fontSize = String.valueOf(Float.parseFloat(labelElement.attributeValue("FontSize")) * 96f / 72f * pyX);
//                                String fontSize = String.valueOf(Float.parseFloat(labelElement.attributeValue("FontSize")) *  pyX);
                                String fontName = labelElement.attributeValue("FontName");
                                boolean fontBold = labelElement.attributeValue("FontBold").equalsIgnoreCase("True");
                                boolean fontItalic = labelElement.attributeValue("FontItalic").equalsIgnoreCase("True");
                                boolean fontStrikeout = labelElement.attributeValue("FontStrikeout").equalsIgnoreCase("True");
                                boolean fontUnderline = labelElement.attributeValue("FontUnderline").equalsIgnoreCase("True");
                                String foreColor = labelElement.attributeValue("ForeColor");
                                String itemBackColor = labelElement.attributeValue("BackColor");
                                String tagString = labelElement.attributeValue("TagString");

                                JsonObject newItem = new JsonObject();
                                if(StringUtils.isNotEmpty(itemBackColor)){
                                    if(itemBackColor.equalsIgnoreCase("white")){
                                        itemBackColor = "#FFFFFF";
                                        newItem.addProperty("backColor", itemBackColor);
                                    } else if(itemBackColor.equalsIgnoreCase("black")){
                                        itemBackColor = "#000000";
                                        newItem.addProperty("backColor", itemBackColor);
                                    } else if(itemBackColor.equalsIgnoreCase("Transparent")){
//                                        newJson.addProperty("backColor", backColor);
                                    } else {
                                        newItem.addProperty("backColor", itemBackColor);
                                    }
                                } else {
//                                    newJson.addProperty("backColor", "#FFFFFF");
                                }


                                if(StringUtils.isNotEmpty(foreColor)){
                                    if(foreColor.equalsIgnoreCase("white")){
                                        foreColor = "#FFFFFF";
                                        newItem.addProperty("foreColor", foreColor);
                                    } else if(foreColor.equalsIgnoreCase("black")){
                                        foreColor = "#000000";
                                        newItem.addProperty("foreColor", foreColor);
                                    } else if(foreColor.equalsIgnoreCase("Transparent")){
//                                        newJson.addProperty("backColor", backColor);
                                    } else {
                                        newItem.addProperty("foreColor", foreColor);
                                    }
                                } else {
//                                    newJson.addProperty("backColor", "#FFFFFF");
                                }


                                newItem.addProperty("type", "text");
                                newItem.addProperty("tagString", tagString);
                                newItem.addProperty("text", text);
                                newItem.addProperty("name", name);
                                newItem.addProperty("textAlign", textAlign);
                                newItem.addProperty("fontName", fontName);
                                newItem.addProperty("fontSize", fontSize);
                                newItem.addProperty("fontBold", fontBold);
                                newItem.addProperty("fontItalic", fontItalic);
                                newItem.addProperty("fontStrikeout", fontStrikeout);
                                newItem.addProperty("fontUnderline", fontUnderline);
//                                newItem.addProperty("foreColor", foreColor);
//                                newItem.addProperty("backColor", itemBackColor);
                                newItem.addProperty("posX", posX);
                                newItem.addProperty("posY", posY);
                                newItem.addProperty("height", itemHeight);
                                newItem.addProperty("width", itemWidth);
                                components.add(newItem);

                            }
                        }
                    }
                }

            }

            newJson.add("components", components);

            log.info(newJson.toString());
            FileUtils.writeStringToFile(json, newJson.toString(), "UTF8");

        }

    }


}
